package wei.xinya.pattern.creational.builder;

import java.util.HashMap;
import java.util.Map;

public class MyConfig {

    private Map<String, Object> data;

    private String appName;

    private Long logTime;

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public Long getLogTime() {
        return logTime;
    }

    public void setLogTime(Long logTime) {
        this.logTime = logTime;
    }

    private MyConfig(Builder builder) {
        this.data = builder.data;
        this.appName = builder.appName;
        this.logTime = builder.logTime;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private final Map<String, Object> data = new HashMap<>();

        private String appName;

        private Long logTime;

        public Builder appName(String appName) {
            this.appName = appName;
            return this;
        }

        public Builder logTime(Long logTime) {
            this.logTime = logTime;
            return this;
        }

        public SemiData key(String key) {
            return new SemiData(this, key);
        }

        public MyConfig build() {
            return new MyConfig(this);
        }

        public static class SemiData {
            private final String key;
            private final Builder parent;

            private SemiData(Builder myConfig, String key) {
                this.parent = myConfig;
                this.key = key;
            }

            public Builder value(Object value) {
                parent.data.put(key, value);
                return parent;
            }
        }
    }
}
